<?php
namespace V1\Controller;


class MessageController extends BaseController {
    
     protected $errorCode = array(
        'remove' => array(
            '-1'=>'用户token失效或有误',
            '-10'=>'更新状态失败',
        ) ,
        'index' => array(
            '-1'=>'用户token失效或有误',
            '-10'=>'数据不存在',
        ) ,
        
    );
    /**
     * 个人消息列表
     * 状态：1 正常 0禁用 -1删除
     */
    public function index(){
       $data = array("code" => 0,"msg" => "","info"=>array());
        
       list($uid,$user_token) = $this->checkUserToken();
      
       
       $pageIndex = I('p',1,'intval');
       $pageSize = I('r',5,'intval');
       
       $list =M('CommonMessage')->where(array('userid'=>$uid,'status'=>1,))->order('send_time, update_time desc')->cache(true,60)->page($pageIndex,$pageSize)->select();
       empty($list) && $this->responseError(-10);
       
       
       $update_msg_ids=array();
       foreach ($list['list'] as $key => $value) {
           if(empty($value['send_time'])){
               $list['list'][$key]['isnew']=1;
               $update_msg_ids[]=$value['id'];
           }else{
               $list['list'][$key]['isnew']=0;
           }
           $list['list'][$key]['update_time'] = tranTime($value['update_time']);
           unset($list['list'][$key]['send_time']);
       }
       if(count($update_msg_ids)) {
           $msg_data = array('id' => array('in', $update_msg_ids ),'send_time' => NOW_TIME);
           M('CommonMessage')->save($msg_data);
       }
       
       $data['info'] = $list;
       $data['user_token'] = $user_token;  

       $this->responseResult($data);
    }
    /**
     * 删除消息
     */
    public function remove(){
       $data = array("code" => 0,"msg" => "","info"=>array());
        
       $id = I('id','0');
       if(empty($id)){
            throw new \Api\Library\Exception\BadRequest("参数异常", 1);
       }
       
      list($uid,$user_token) = $this->checkUserToken();

      $result = $this->delete('CommonMessage',array('user_id'=>$uid));
      
      empty($result) && $this->responseError(-10);
      
      $this->responseResult($data);
    }
     
    
}